Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jul 28, 2025

Summary

This PR fixes issue #6290 where the VS Code Language Model API was unable to correctly calculate context size when using models like "copilot - gpt-4.1". The issue was caused by VS Code updating their countTokens API to expect LanguageModelChatMessage2 instead of LanguageModelChatMessage.

Problem

  • When using VS Code LM API with models like "copilot - gpt-4.1", the context size calculation was incorrect
  • System prompt was accurately accounted for, but conversation content had minimal impact on context size
  • The root cause was that VS Code's countTokens method now expects a different message format

Solution

  1. Updated internalCountTokens method to handle both old and new API versions:

    • First attempts to use the message object directly (new API)
    • Falls back to extracting text content and counting tokens as a string if the new API fails
    • Properly handles both LanguageModelChatMessage instances and string inputs
  2. Enhanced calculateTotalInputTokens method:

    • Attempts batch token counting first for better performance
    • Falls back to individual message counting if batch counting fails
    • Ensures compatibility with both API versions
  3. Added comprehensive test coverage:

    • Tests for successful token counting with text content
    • Tests for handling image content with placeholders
    • Tests for API error handling and graceful fallback
    • Tests for the specific scenario where the new API format causes errors

Testing

  • All existing tests pass ✅
  • Added new tests specifically for the fallback behavior ✅
  • Ran full test suite for all API providers to ensure no regressions ✅
  • All linting and type checks pass ✅

Impact

This fix ensures that Roo Code continues to work correctly with VS Code's Language Model API regardless of which version of the API is being used, providing a seamless experience for users.

Fixes #6290


Important

Fixes VS Code LM API token counting by updating methods to handle new API changes and adding a tiktoken fallback.

  • Behavior:
    • Updates internalCountTokens in vscode-lm.ts to handle both LanguageModelChatMessage and LanguageModelChatMessage2.
    • Adds fallback to tiktoken when VS Code API returns 0 or fails.
    • Enhances calculateTotalInputTokens to attempt batch token counting first.
  • Testing:
    • Adds tests in vscode-lm.spec.ts for fallback behavior when VS Code API returns 0, throws an error, or returns negative values.
    • Tests for frequent token updates during streaming.
  • Misc:
    • Adds fallbackToTiktoken method for token counting fallback.
    • Updates logging for better error handling and debugging.

This description was created by Ellipsis for 6722ae2. You can customize this summary. It will automatically update as commits are pushed.

@roomote roomote bot requested review from cte, jr and mrubens as code owners July 28, 2025 11:54
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jul 28, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jul 28, 2025
- Add tiktoken fallback when VSCode API returns 0, negative, or invalid values
- Add tiktoken fallback when VSCode API is unavailable or throws errors
- Update token counting to provide more frequent updates during streaming (every 100 chars)
- Add comprehensive tests for fallback behavior

Fixes #6112
@roomote roomote bot force-pushed the feature/fix-vscode-lm-token-counting branch from b51d5c3 to 6722ae2 Compare July 28, 2025 16:40
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jul 28, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Jul 28, 2025
@daniel-lxs
Copy link
Member

Closing as duplicate

@daniel-lxs daniel-lxs closed this Jul 30, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jul 30, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Needs Preliminary Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

VS Code LM API unable to get the correct context size

4 participants